/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: mysql.c
> Author:cww
> Mail: 
> Created Time: 2024年09月20日 星期五 15时31分17秒
***************************************************/
#include "mysql.h"
#include "cJSON.h"

#include "mysql.h"

Node *insert_data(Info infor, Login pro, Instrument data, Message message)
{
    Node *newnode = (Node*)malloc(sizeof(Node));
    assert(newnode!= NULL);

    newnode->infor = infor;
    newnode->pro = pro;
    newnode->data = data;
    newnode->message = message;
    newnode->next = NULL;

    return newnode;
}

MYSQL *connect_to_mysql()
{
    MYSQL *connect = NULL, *res = NULL;
    connect = mysql_init(NULL);

    res = mysql_real_connect(connect, "localhost", "root", "1", "information1", 0, NULL, 0);
    if (res == NULL)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }

    return connect;
}

// 插入 Login 数据
void insert_login_data(MYSQL *connect, Login pro)
{
    char sql[1024] = {0};
    int ret = 0;

    sprintf(sql, "insert into login values('%s','%s')", pro.account, pro.password);
    ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

// 插入 Info 数据
void insert_info_data(MYSQL *connect, Info infor)
{
    char sql[1024] = {0};
    int ret = 0;

    sprintf(sql, "insert into info (temp, humidity, O2, CO2, CH4, CO, time, pt, principal, state) values ('%f', '%f', '%f', '%f', '%f', '%f', '%s', '%s', '%s', '%s')", infor.temp, infor.humidity, infor.O2, infor.CO2, infor.CH4, infor.CO, infor.time, infor.pt, infor.principal, infor.state);
    ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

// 插入 Instrument 数据
void insert_instrument_data(MYSQL *connect, Instrument data)
{
    char sql[1024] = {0};
    int ret = 0;

    sprintf(sql, "insert into Instrument values(%d,'%s','%s','%s','%s','%s','%s')", data.id, data.name, data.contact_way, data.Instrument_number, data.bor_time, data.ret_time, data.state);
    ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

//插入人员信息
void insert_message_data(MYSQL *connect, Message message, char *table)
{
    char sql[1024] = {0};
    int ret = 0;

    if (strcmp(table, "Safety_officer") == 0)
    {
        sprintf(sql, "insert into Safety_officer values('%s','%s','%s','%s','%s','%s')", message.name, message.gender, message.age, message.telephone, message.account, message.password);
    }
    else if (strcmp(table, "manager") == 0)
    {
        sprintf(sql, "insert into manager values('%s','%s','%s','%s','%s','%s')", message.name, message.gender, message.age, message.telephone, message.account, message.password);
    }
    ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

// 删除数据
void delete_data_from_table(MYSQL *connect, char *table, char *field, char *value)
{
    char sql[1024] = {0};
    sprintf(sql, "delete from %s where %s = '%s'", table, field, value);
    int ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

// 更新数据
void update_table_data(MYSQL *connect, char *table, char *field, char *value, char *where)
{
    char sql[1024] = {0};
    sprintf(sql, "update %s set %s = '%s' where %s", table, field, value, where);
    int ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        printf("%s\n", mysql_error(connect));
        mysql_close(connect);
        exit(-1);
    }
}

// 查询数据函数，并返回 JSON 格式的结果
cJSON *query_data_as_json(MYSQL *connect, char *table, char *query_condition)
{
    char sql[1024] = {0};
    if (strcmp(table, "login") == 0)
    {
        sprintf(sql, "SELECT * FROM login %s", query_condition);
    }
    else if (strcmp(table, "info") == 0)
    {
        sprintf(sql, "SELECT * FROM info %s", query_condition);
    }
    else if (strcmp(table, "Instrument") == 0)
    {
        sprintf(sql, "SELECT * FROM Instrument %s", query_condition);
    }
    else
    {
        return cJSON_CreateObject();
    }
    int ret = mysql_query(connect, sql);
    if (ret!= 0)
    {
        cJSON *errorJson = cJSON_CreateObject();
        cJSON_AddStringToObject(errorJson, "error", mysql_error(connect));
        return errorJson;
    }
    MYSQL_RES *result = mysql_store_result(connect);
    if (result == NULL)
    {
        cJSON *errorJson = cJSON_CreateObject();
        cJSON_AddStringToObject(errorJson, "error", mysql_error(connect));
        return errorJson;
    }
    cJSON *root = cJSON_CreateArray();
    MYSQL_ROW row;
    int num_fields = mysql_num_fields(result);
    while ((row = mysql_fetch_row(result)))
    {
        cJSON *item = cJSON_CreateObject();
        for (int i = 0; i < num_fields; i++)
        {
            cJSON_AddStringToObject(item, mysql_fetch_field(result)->name, row[i]? row[i] : "NULL");
        }
        cJSON_AddItemToArray(root, item);
    }
    mysql_free_result(result);
    return root;
}

// 将 JSON 对象转换为字符串并返回
char *json_to_string(cJSON *json)
{
    char *str = cJSON_Print(json);
    return str;
}

char *query_data(MYSQL *connect, char *table, char *query_condition) {
    char sql[1024] = {0};
    sprintf(sql, "SELECT * FROM %s %s", table, query_condition);
    int ret = mysql_query(connect, sql);
    if (ret!= 0) {
        cJSON *errorJson = cJSON_CreateObject();
        cJSON_AddStringToObject(errorJson, "error", mysql_error(connect));
        return json_to_string(errorJson);
    }
    MYSQL_RES *result = mysql_store_result(connect);
    if (result == NULL) {
        cJSON *errorJson = cJSON_CreateObject();
        cJSON_AddStringToObject(errorJson, "error", mysql_error(connect));
        return json_to_string(errorJson);
    }
    cJSON *root = cJSON_CreateArray();
    MYSQL_ROW row;
    int num_fields = mysql_num_fields(result);
    while ((row = mysql_fetch_row(result))) {
        cJSON *item = cJSON_CreateObject();
        for (int i = 0; i < num_fields; i++) {
            cJSON_AddStringToObject(item, mysql_fetch_field(result)->name, row[i]? row[i] : "NULL");
        }
        cJSON_AddItemToArray(root, item);
    }
    mysql_free_result(result);
    return json_to_string(root);
}





